package com.example.myapplication.Garbagesorting;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.myapplication.Config.OKHttpTool;
import com.example.myapplication.R;
import com.example.myapplication.ShuJu.LeavecommentBean;
import com.example.myapplication.ShuJu.NewsdetailscategoryBean;

import org.json.JSONException;
import org.json.JSONObject;

public class NewsdetailscategoryActivity extends AppCompatActivity {

    private String newsdetailscategoryID;
    private TextView newsdetailscategoryTitle;
    private TextView newsdetailscategoryconten;
    private TextView newsdetailscategoryRen;
    private TextView newsdetailscategorydata;
    private Button newsdetailscategorybloak;
    private Button but_newsdetailscategory;
    private EditText ed_newsdetailscategoryRen;
    private NewsdetailscategoryBean newsdetailscategoryBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_newsdetailscategory);
        Intent intent = getIntent();
        newsdetailscategoryID = intent.getStringExtra("NewsdetailscategoryID");
        initview();
        //返回
        but_bloak();
        //新闻详情
        sendata();
    }

    private void leavecomment() {
        but_newsdetailscategory.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String Content = ed_newsdetailscategoryRen.getText().toString();
                if(Content.isEmpty()){
                    Content =" ";
                }
                JSONObject jsonObject = new JSONObject();
                try {
                    jsonObject.put("newsId",String.valueOf(newsdetailscategoryBean.getData().getId()));
                    jsonObject.put("content",Content);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                OKHttpTool.doNetRequest("prod-api/api/garbage-classification/news-comment",
                        OKHttpTool.POST,
                        LeavecommentBean.class, new OKHttpTool.NetCallback() {
                            @Override
                            public void onSuccess(Object obj) {
                                LeavecommentBean leavecommentBean = (LeavecommentBean)obj;
                                if(leavecommentBean.getCode()==200){
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            Toast.makeText(NewsdetailscategoryActivity.this,"评论成功",Toast.LENGTH_SHORT);
                                        }
                                    });
                                }
                            }

                            @Override
                            public void onFailure(String msg) {

                            }
                        },jsonObject.toString());
            }
        });
    }

    private void but_bloak() {
        newsdetailscategorybloak.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                NewsdetailscategoryActivity.this.finish();
            }
        });
    }

    private void sendata() {
        OKHttpTool.doNetRequest("prod-api/api/garbage-classification/news/" + newsdetailscategoryID,
                OKHttpTool.GET,
                NewsdetailscategoryBean.class, new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                         newsdetailscategoryBean = (NewsdetailscategoryBean) obj;
                        if (newsdetailscategoryBean.getCode() == 200) {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    newsdetailscategoryTitle.setText(newsdetailscategoryBean.getData().getTitle());
                                    newsdetailscategoryRen.setText(newsdetailscategoryBean.getData().getAuthor());
                                    newsdetailscategorydata.setText(newsdetailscategoryBean.getData().getCreateTime());
                                    newsdetailscategoryconten.setText(Html.fromHtml(newsdetailscategoryBean.getData().getContent()));
                                    //发表评论
                                    leavecomment();
                                }
                            });
                        }
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                });
    }

    private void initview() {
        newsdetailscategoryTitle = findViewById(R.id.newsdetailscategoryTitle);
        newsdetailscategorybloak = findViewById(R.id.newsdetailscategorybloak);
        newsdetailscategoryconten = findViewById(R.id.newsdetailscategoryconten);
        newsdetailscategoryRen = findViewById(R.id.newsdetailscategoryRen);
        newsdetailscategorydata = findViewById(R.id.newsdetailscategorydata);
        but_newsdetailscategory = findViewById(R.id.but_newsdetailscategory);
        ed_newsdetailscategoryRen = findViewById(R.id.ed_newsdetailscategoryRen);
    }
}